<?php
/**
 * Factory to create the correct ValueConverterFactory.
 *
 * @author  Stephan Schmidt <stephan.schmidt@schlund.de>
 * @author  Frank Kleine <frank.kleine@schlund.de>
 */
XJConfLoader::load('converters.factories.ValueConverterFactory',
                   'converters.factories.PrimitiveValueConverterFactory',
                   'converters.factories.ArrayValueConverterFactory',
                   'converters.factories.ConstructorValueConverterFactory',
                   'converters.factories.FactoryMethodValueConverterFactory',
                   'definitions.Definition'
);
/**
 * Factory to create the correct ValueConverterFactory.
 *
 * @package     XJConf
 * @subpackage  converters
 */
class ValueConverterFactoryChain
{
    /**
     * contains a list of all available ValueConverterFactorys
     *
     * @var  array<ValueConverterFactory>
     */
    private static $factories = array();

    /**
     * add a ValueConverterFactory to list of known ValueConverterFactorys
     *
     * @param  ValueConverterFactory  $factory
     */
    public static function push(ValueConverterFactory $factory)
    {
        array_push(self::$factories, $factory);
    }

    /**
     * return the correct ValueConverterFactory depending on the given definition
     *
     * @param   Definition             $def
     * @return  ValueConverterFactory
     */
    public static function getFactory(Definition $def)
    {
        foreach (self::$factories as $factory) {
            if ($factory->isResponsible($def) == true) {
                return $factory;
            }
        }

        return null;
    }

}
// initialize the ValueConverterFactoryChain
ValueConverterFactoryChain::push(new PrimitiveValueConverterFactory());
ValueConverterFactoryChain::push(new ArrayValueConverterFactory());
ValueConverterFactoryChain::push(new ConstructorValueConverterFactory());
ValueConverterFactoryChain::push(new FactoryMethodValueConverterFactory());
?>